<?php

namespace App\Console\Commands;

use App\Models\StudentInfo;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class getStudentsInfo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'get:students-info {year}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $year = $this->argument('year');
        if (!$year) {
            echo '请输入需导入的年级';
            exit();
        }

        $name = $this->ask('是否要导入' . $year . '级的学生资料 （y/n）');
        if ($name == 'y') {
            DB::table('students')->where('u_code', 'like', $year . '%')->orderBy('id')->chunk(100, function ($users) {
                foreach ($users as $user) {
                    $request= \College::user($user->u_code);
                    $result = $request[0];
                    if ($result) {
                        $student = StudentInfo::query()->where('xh', $result->xh)->first();
                        if (!$student) {
                            $student = new  StudentInfo();
                        }
                        $student->student_id = $user->id;
                        $student->xh = $result->xh;
                        $student->xm = $result->xm;
                        $student->xbm = $result->xbm;
                        $student->csrq = $result->csrq;
                        $student->sfzjh = $result->sfzjh;
                        $student->mzm = $result->mzm;
                        $student->zzmmm = $result->zzmmm;
                        $student->ywb = $result->ywb;
                        $student->school = "天津市大学软件学院";
                        $student->type = "专升本";
                        $student->lxdh = $result->lxdh;
                        $student->zyfx = '软件工程';
                        $student->xsh = $this->getSchool($result->xh);
                        $student->nj = $result->nj;
                        $student->save();
                        echo $student->xm."\n";
                    }
                }
            });

        }
        //
    }
    public function getSchool($xh){
      $num =   substr($xh,2,3);
//      var_dump($num);die;
      switch ($num){
          case "502";
          return '天津工业大学';
          case "503";
              return '天津师范大学';
          case "504";
              return '天津理工大学';
          case "505";
              return '天津城建大学';
          case "506";
              return '天津职业技术师范大学';
              default;
              return '天津大学软件学院';
      }
    }
}
